﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using MySql.Data;
using MySql.Data.MySqlClient;

namespace Test.TestCustomControl {
    public partial class TestDgvPageForm : Form {

        private static string strConn = "Server=localhost;Database=test; User=root;" +
                         "Password=123456;";

        private int allCount = 0;   //数据表总行数
        private int pageCount = 0;  //数据表总页数
        private int pageSize = 2;   //显示的行数
        private int page = 1;

        public TestDgvPageForm() {
            InitializeComponent();
            using (MySqlConnection connection = new MySqlConnection(strConn)) {
                try {
                    MySqlDataAdapter da = new MySqlDataAdapter("select * from t_dgv;", connection);
                    DataTable dt = new DataTable();
                    da.Fill(dt);
                    allCount = dt.Rows.Count;
                    pageCount = allCount % pageSize;
                    if (pageCount == 0) {
                        pageCount = allCount / pageSize;
                    } else {
                        pageCount = allCount / pageSize + 1;
                    }
                    this.label1.Text = string.Format("共 {0} 页", pageCount);
                    this.label3.Text = string.Format("/{0}页", pageCount);
                    ShowPage(1);
                    UpdatePage();

                } catch (Exception err) {
#if DEBUG
                    Console.WriteLine("查询错误！错误信息：{0}", err);
#endif
                }
            }

        }

        private void TestDgvPageForm_Load(object sender, EventArgs e) {
//            using (MySqlConnection connection = new MySqlConnection(strConn)) {
//                try {
//                    MySqlDataAdapter da = new MySqlDataAdapter("select * from t_dgv;", connection);
//                    DataTable dt = new DataTable();
//                    da.Fill(dt);
//                    allCount = dt.Rows.Count;
//                    pageCount = allCount % pageSize;
//                    if (pageCount == 0) {
//                        pageCount = allCount / pageSize;
//                    } else {
//                        pageCount = allCount / pageSize + 1;
//                    }
//                    this.label1.Text = string.Format("共 {0} 页",pageCount);
//                    this.label3.Text = string.Format("/{0}页", pageCount);
//                    ShowPage(1);
//                    UpdatePage();

//                } catch (Exception err) {
//#if DEBUG
//                    Console.WriteLine("查询错误！错误信息：{0}", err);
//#endif
//                }
//            }
        }

        /// <summary>
        /// 分页显示数据表内容
        /// </summary>
        /// <param name="start"></param>
        /// <param name="end"></param>
        private void ShowPage(int page) {
            try {
                MySqlConnection conn = new MySqlConnection(strConn);
                string sql = null;
                int firstRow = 0;
                int backRow = 0;
                if (page == 1) {
                    firstRow = page;
                    backRow = page * pageSize;
                } else {
                    firstRow = (page - 1) * pageSize + 1;
                    backRow = (page - 1) * pageSize + pageSize;
                }
                sql = string.Format("select * from t_dgv where id between {0} and {1};", firstRow, backRow);

                MySqlDataAdapter da = new MySqlDataAdapter(sql, conn);
                DataSet ds = new DataSet();
                da.Fill(ds);
                this.dataGridView1.DataSource = ds.Tables[0].DefaultView;
                ds = null;
            } catch (Exception err) {
#if DEBUG
                Console.WriteLine("查询分页错误！错误信息：{0}", err);
#endif
            }
        }

        /// <summary>
        /// 更新页码
        /// </summary>
        private void UpdatePage() {
            this.label2.Text = string.Format("当前第 {0} 页", page);
            this.textBox1.Text = page.ToString();
        }

        private void btn_FirstPage_Click(object sender, EventArgs e) {
            page = 1;
            ShowPage(1);
            UpdatePage();
        }

        private void btn_BackPage_Click(object sender, EventArgs e) {
            page = pageCount;
            ShowPage(pageCount);
            UpdatePage();
        }

        private void btn_NextPage_Click(object sender, EventArgs e) {
            page += 1;
            if (page > pageCount) {
                page = pageCount;
            }
            ShowPage(page);
            UpdatePage();
        }

        private void button1_Click(object sender, EventArgs e) {
            page -= 1;
            if (page < 1) {
                page = 1;
            }
            ShowPage(page);
            UpdatePage();
        }

        private void textBox1_TextChanged(object sender, EventArgs e) {
            try {
                page = Convert.ToInt32(this.textBox1.Text);
                ShowPage(page);
                UpdatePage();
            } catch (Exception) {
            }
        }



    }
}











